package com.genesis.yunnanji.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntegerRes;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;

/**
 * 获取资源工具
 */
public class ResourceUtil {

    public static int getResourceId(Context context, String name, String type) {
        return context.getResources().getIdentifier(name, type, AppConfig.PACKAGE_NAME);
    }

    public static Uri getResourceUri(int resId, String packageName) {
        return Uri.parse("res://" + packageName + "/" + resId);
    }

    public static Uri getResourceUri(int resId) {
        return getResourceUri(resId, AppConfig.PACKAGE_NAME);
    }

    public static int getColor(Context context, @ColorRes int resId) {
        return ContextCompat.getColor(context, resId);
    }

    public static Drawable getDrawable(Context context, @DrawableRes int resId) {
        return ContextCompat.getDrawable(context, resId);
    }

    public static String getString(Context context, @StringRes int resId) {
        return context.getResources().getString(resId);
    }

    public static String getString(Context context, @StringRes int resId, Object... formatArgs) {
        return context.getResources().getString(resId, formatArgs);
    }

    public static int getInteger(Context context, @IntegerRes int resId) {
        return context.getResources().getInteger(resId);
    }

    public static Drawable getDrawableFromAttr(Context context, @AttrRes int attrId) {
        TypedArray typedArray = context.obtainStyledAttributes(new int[]{attrId});
        Drawable drawable = typedArray.getDrawable(0);
        typedArray.recycle();
        return drawable;
    }

    public static int getColorFromAttr(Context context, @AttrRes int attrId, @ColorInt int defValue) {
        TypedArray typedArray = context.obtainStyledAttributes(new int[]{attrId});
        int color = typedArray.getColor(0, defValue);
        typedArray.recycle();
        return color;
    }

}
